package com.example.demo.servyou;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.StrUtil;
import com.example.demo.servyou.entity.Document;
import com.example.demo.servyou.mapper.DocumentMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@SpringBootTest
public class Dzfp {

    @Autowired
    private DocumentMapper mapper;


    private static final String xmlHead = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    private static final String file_name = "/Users/zouyao/workspace/servyou/compare_excel/doc/model222222.xml";

    @Test
    public void t1(){

        // 1. 收集数据
        List<Document> documents = mapper.selectList(null);

        // 2. 将数据按照 owenr 分组
        Map<String, List<Document>> docMap = documents.stream()
                .collect(Collectors.groupingBy(
                        k -> {
                            String xh = k.getXh();

                            // 子节点按照根结点对齐
                            if (xh.contains("."))
                                return xh.split("\\.")[0];

                            // 普通节点直接对齐
                            return xh;
                        },

                        Collectors.mapping(val -> val, Collectors.toList()))
                );




        // 4. 创建文件
        File file = FileUtil.touch(file_name);
        FileWriter writer = new FileWriter(file);

        // 5. xml头部要素填充
        writer.append(xmlHead);

        // 3. 循环处理每一个文件
        docMap.forEach( (k,v) -> {
            // 2. 备注信息补充
            writer.append("<!-- " + k + "-->" + "\t\n");
            // 3. 要素主体信息
            List<String> docs = xmlFormat(v);
            for (String doc : docs) {
                writer.append(doc+"\t\n");
            }

        });


    }



    public static List<String> xmlFormat(List<Document> documents){

        // 初始化链表
        List<String> labels = new ArrayList<>(documents.size());
        for (int i = 0; i < documents.size() + 1; i++) {
            labels.add("");
        }


        for (int i = 0; i < documents.size() ; i++) {
            Document doc = documents.get(i);

            // 处理要素
            String labelStart = "<" + doc.getYsmYw() + ">";
            String labelEnd   = "</" + doc.getYsmYw() + ">";
            String labelInner = doc.getYsmZw();

            // 添加 序号
           String inner_xh = "<!--" + "   " +doc.getXh()  + "-->";

            // 判断是否为根结点
            if (!doc.getXh().contains(".")){
                labels.add(0,labelStart);
                labels.add(documents.size() + 1 ,labelEnd);
                continue;
            }

            // 普通要素
            String defalutEle = labelStart + labelInner + labelEnd;
            labels.add(i+1,inner_xh + defalutEle);
            labels.add("\n\t");

        }

        // 清除要素
        return labels.stream().filter( x -> !StrUtil.isEmpty(x)).collect(Collectors.toList());
    }

    public static List<Element> xmlFormatElement(List<Document> documents){

        // 初始化链表
        List<Element> labels = new ArrayList<>(documents.size());
        for (int i = 0; i < documents.size() + 1; i++) {
            labels.add(new Element());
        }


        for (int i = 0; i < documents.size() ; i++) {
            Document doc = documents.get(i);

            // 处理要素
            String labelStart = "<" + doc.getYsmYw() + ">";
            String labelEnd   = "</" + doc.getYsmYw() + ">";
            String labelInner = doc.getYsmZw();

            // 添加 序号
            String inner_xh = "<!--" + "   " +doc.getXh()  + "-->";

            // 判断是否为根结点
            if (!doc.getXh().contains(".")){
                labels.add(0,new Element(doc.getXh(),labelStart));
                labels.add(documents.size() + 1 ,new Element(doc.getXh(),labelEnd));
                continue;
            }

            // 普通要素
            String defalutEle = labelStart + labelInner + labelEnd;
            labels.add(i+1,new Element(doc.getXh(),inner_xh + defalutEle));
            labels.add(new Element("","\n\t"));

        }

        // 清除要素
        return labels.stream().filter( x -> !StrUtil.isEmpty(x.getEle())).collect(Collectors.toList());
    }

}

@AllArgsConstructor
@NoArgsConstructor
@Data
class Element {

    private String xh;

    private String ele;
}

